package luanvan.app.foodnote.library;

import java.util.List;

import luanvan.app.foodnote.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FoodAdapter extends ArrayAdapter<Food> {
	
	public static int activity;
	
	private List<Food> foods;
	public FoodAdapter(Context context, int textViewResourceId, List<Food> foods) {
		super(context, textViewResourceId, foods);
		// TODO Auto-generated constructor stub
		this.foods = foods;
	}

	@Override
	public int getCount(){ 
		return foods != null ? foods.size() : 0;
	}
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		Holder holder = null;
		if(view == null){
			
			holder = new Holder();
			
			LayoutInflater layoutInflater = (LayoutInflater) getContext().
					getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = layoutInflater.inflate(R.layout.row, null);
			
			
			// cache the views
			
			holder.setIconImageView((ImageView) view.findViewById(R.id.icon));
			holder.setIdTextView((TextView) view.findViewById(R.id.fid));
			holder.setLatitudeTextView((TextView) view.findViewById(R.id.lat));
			holder.setLongitudeTextView((TextView) view.findViewById(R.id.lng));
			holder.setCityTextView((TextView) view.findViewById(R.id.city));
			holder.setTypeFoodIdTextView((TextView) view.findViewById(R.id.type_food_id));
			holder.setFoodNameTextView((TextView) view.findViewById(R.id.foodname));
			holder.setPlaceNameTextView((TextView) view.findViewById(R.id.placename));
			holder.setAddresstTextView((TextView) view.findViewById(R.id.address));
			
			
			view.setTag(holder);
		} else {
			holder = (Holder) view.getTag();
		}
		// Set properties
		final Food food = foods.get(position);
		
		holder.getIdTextView().setText(food.getId());
		
		holder.getLatitudeTextView().setText(String.valueOf(food.getLatitude()));
		
		holder.getLongitudeTextView().setText(String.valueOf(food.getLongitude()));
		
		holder.getCityTextView().setText(food.getCity());
		
		holder.getTypeFoodIdTextView().setText(food.getType_food_id());
		
		holder.getFoodNameTextView().setText(food.getFood_name());
		
		holder.getPlaceNameTextView().setText(food.getPlace_name());
		
		holder.getAddresstTextView().setText(food.getAddress());
		
		if(Integer.valueOf(food.getType_food_id()) == 1){
			holder.getIconImageView().setImageResource(R.drawable.icon_fast_food);
		} else if(Integer.valueOf(food.getType_food_id()) == 2){
			holder.getIconImageView().setImageResource(R.drawable.dessert);
		} else if(Integer.valueOf(food.getType_food_id()) == 3){
			holder.getIconImageView().setImageResource(R.drawable.drink_icon);
		}
		
		
		return view;
	}
	
}

